package com.source.multithread.blockkingqueue;

import com.source.multithread.annotation.Student;
import lombok.SneakyThrows;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author wengqy
 * @Date 2021/1/8
 */
public class BlockDemo {
    public static void main(String[] args) throws InterruptedException {

      /*  BlockingQueue<String> syn = new SynchronousQueue();
        System.out.println(syn.isEmpty());
        syn.offer("aa");
        System.out.println(syn.isEmpty());

        TimeUnit.SECONDS.sleep(2);
        String take = syn.take();
        System.out.println(take);*/



        BlockingQueue<String> syn = new SynchronousQueue<>();

        new Thread(new Productor(syn)).start();
        new Thread(new Consumer(syn)).start();


    }
}



class Productor implements Runnable{
    BlockingQueue<String> syn ;

    public Productor(  BlockingQueue<String> syn ){
        this.syn = syn;
    }

    @Override
    public void run() {
        while(true){
            int num = new Random().nextInt(100);
            System.out.println("1随机生产一个数据： ");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
             syn.offer(num+"");
            System.out.println("2随机生产一个数据： "+num);
        }
    }
}


class Consumer implements Runnable{
    BlockingQueue<String> syn;

    public Consumer(  BlockingQueue<String> syn ){
        this.syn = syn;
    }
    @Override
    public void run() {
        while(true){
            try {
                System.out.println("1消费一个产品为：");
                String res = syn.take();
                System.out.println("2消费一个产品为："+res);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
